package ntd.util.str.bo;

import cn.hutool.core.exceptions.ExceptionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import lombok.Data;
import lombok.experimental.Accessors;

@Data
@Accessors(chain = true)
public class CutMsg {
    private String code;
    private String startTag;
    private String endTag;
    private Integer length;
    private String result;

    public int getEndIndex(int startIndex, String currentStr) {
        if (ObjectUtil.isNotNull(length)) {
            int endIndex = startIndex + this.length;
            if (endIndex > currentStr.length()) {
                return -1;
            }
            return endIndex;
        }
        if (StrUtil.isNotEmpty(endTag)) {
            int endIndex = currentStr.indexOf(this.endTag, startIndex + this.startTag.length());
            if (endIndex > -1) {
                endIndex += this.endTag.length();
            }
            return endIndex;
        }
        throw ExceptionUtil.wrapRuntime("utMsg 数据结尾标记异常，请检查配置参数");
    }
}
